import {createVitePlugins} from './config/vite/plugins';
import {resolve} from 'path';
import {loadEnv, ConfigEnv, UserConfigExport} from 'vite';

const CopyWebpackPlugin = require("copy-webpack-plugin") //引入插件


const pathResolve = (dir: string) => {
    return resolve(process.cwd(), '.', dir);
};

// https://vitejs.dev/config/
export default function ({command}: ConfigEnv): UserConfigExport {
    const isProduction = command === 'build';
    const root = process.cwd();
    //const env = loadEnv(mode, process.cwd())
    //const {VITE_APP_EDU_API}=env;

    return {
        root,
        resolve: {
            alias: [
                {
                    find: 'vue-i18n',
                    replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
                },
                // /@/xxxx => src/xxxx
                {
                    find: /\/@\//,
                    replacement: pathResolve('src') + '/',
                },
                // /#/xxxx => types/xxxx
                {
                    find: /\/#\//,
                    replacement: pathResolve('types') + '/',
                },
            ],
        },
        server: {
            host: true,
            // port: 8005,
            hmr: true,
            proxy: {
                '/hbmd_wx': {
                    //部署需要改 todo
                    // target: 'http://hegang.natapp1.cc/hbmy_wx/',
                    // target: 'https://proxy.hbmzu.edu.cn/hbmy_wx',
                    //http://localhost:5173/login?account=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0eXBlIjoiMiIsImV4cCI6MTY5NTIxMDM3OCwiYWNjb3VudCI6IjIwMjExNDEzNyJ9.zl6fMIprWgImRmO3TdTdMNscWqFvPnZfS1lRiaReeAA
                    // target: 'http://localhost:8081/hbmd_wx_war_exploded',
                    // target: 'http://10.212.244.75:8080/hbmd_wx_war/',
                    // target: 'http://211.67.33.138:8080/hbmd_wx/',
                    target: 'http://127.0.0.1:8082/hbmd_wx_war_exploded/',
                    secure: false,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/hbmd_wx/, '')
                },
                '/filepath': {
                    //部署需要改 todo
                    // target: 'http://hegang.natapp1.cc/filepath/',
                    target: 'https://proxy.hbmzu.edu.cn/',
                    secure: false,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/filepath/, '')
                },
                '/jwfilepath': {
                    //部署需要改 todo
                    // target: 'http://hegang.natapp1.cc/filepath/',
                    // target: 'http://127.0.0.1:8084/jwfilepath/',
                    target: 'https://proxy.hbmzu.edu.cn/jwfilepath/',
                    secure: false,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/jwfilepath/, '')
                },
                '/new_module': {
                    // target:"http://211.67.33.138:8080/",
                    // target:"https://6220f7ed.r9.cpolar.top",
                    target: 'http://localhost:8081/',
                    changeOrigin: true,
                    // rewrite: (p) => p.replace(/^\/new_module/, '')
                },
                '/edu_war_exploded': {
                    target: 'http://211.67.33.138:8080/',
                    changeOrigin: true,
                },
            }
        },
        plugins: createVitePlugins(isProduction),
        // todo 12/7
        //base: '',
        //base: 'eduwx',
        build: {
            minify: 'terser',
            terserOptions: {
                compress: {
                    //生产环境时移除console
                    drop_console: false,
                    // drop_console: false,
                    drop_debugger: true,
                },
            },
            // publicPath: '/sportWx/',
            // assetsPublicPath: '/sportWx/'

            // outDir: 'sportWx',
            // assetsDir: '././',
            // publicPath: '../',
            // assetsPublicPath: '../'
        },
        css: {
            preprocessorOptions: {
                scss: {
                    // 配置 nutui 全局 scss 变量
                    additionalData: `@import "@nutui/nutui/dist/styles/variables.scss";@import '/@/styles/mixin.scss';`,
                },
            },
        },
        // @ts-ignore
        configureWebpack: {
            plugins: [
                new CopyWebpackPlugin({
                    patterns: [{
                        from: "./public",
                        to: 'public'
                    }]
                })
            ]
        }
    };
}
